package com.ymatou.productstock.api.testcase;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.productstock.api.parameter.CatalogBean;
import com.ymatou.productstock.api.service.FreezeAllocateStockCall;
import com.ymatou.productstock.api.service.UnfreezeAllocateStockCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.ProductStockCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductActivityStock;
import com.ymttest.database.model.YmtProductFreezeStock;
import com.ymttest.database.model.YmtProductFreezeStockLog;
import com.ymttest.database.mongo.ProductActivityStockInfoWapper;
import com.ymttest.database.mongo.ProductStockInfoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductFreezeStockLogWapper;
import com.ymttest.database.sqlwapper.YmtProductFreezeStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_UnfreezeAllocateStock {
	private static FreezeAllocateStockCall freezeAllocateStockCall = new FreezeAllocateStockCall();
	private static UnfreezeAllocateStockCall unfreezeAllocateStockCall = new UnfreezeAllocateStockCall();
	
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static int activityStartId = Integer.parseInt(EnvSetup.getData("productActivityStartId"));
	private static String activityUser = EnvSetup.getData("AdminLoginUser");
	private static String activityPswd = EnvSetup.getData("AdminLoginPswd");
	private static String dateformat = "HHmmssSSS";
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("解冻调拨库存");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(unfreezeAllocateStockCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_001() {
		Logger.start(true, "解冻调拨库存-进活动");
		try{
			//发布商品
			String productId = SellerProductCallService.addNewProductAndApproveActivity(sellerId,
					sellerName,activityUser,activityPswd,String.valueOf(activityStartId),null);
			
			int productInActivityId = new YmtProductsInActivityWapper().selectByProductId(productId).getIproductinactivityid();
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogsId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();//共享库存
			String SKU = ymtCatalog.getsSKU();
			
			YmtProductActivityStockWapper ymtActivityStockWapper = new YmtProductActivityStockWapper();
			YmtProductActivityStock ymtActivityStock = ymtActivityStockWapper.selectByProductId(productId).get(0);
			int activityNum = ymtActivityStock.getIstocknum();//活动库存
			
			
			//冻结库存
			String FreezeId = "DJ"+YMTDateUtil.getSysTime(dateformat);
			int FreezeNum = 1;
			
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("UserId",sellerId);
			updateMap.put("FreezeId",FreezeId);
			updateMap.put("ProductId",productId);
			updateMap.put("ActivityId",activityStartId);
			updateMap.put("ProductInActivityId",productInActivityId);
			updateMap.put("IsFbxProduct",false);
			updateMap.put("StoreType",0); //冻结库存
			updateMap.put("FreezeCatalogList[0]->CatalogId",catalogsId);
			updateMap.put("FreezeCatalogList[0]->Sku",SKU);
			updateMap.put("FreezeCatalogList[0]->FreezeNum",FreezeNum);
			
			JSONObject retBean = freezeAllocateStockCall.callAndGetReturnData(updateMap);
			Logger.verifyEquals( 200,retBean.getInt("Code"), "响应报文-返回码");
			
			//解冻库存
			updateMap.put("OperateType", 2);
			JSONObject unfreezeBean = unfreezeAllocateStockCall.callAndGetReturnData(updateMap);
			Logger.verifyEquals( 200,unfreezeBean.getInt("Code"), "响应报文-返回码");
			
			//check sql
			//SQL-YmtProductFreezeStock
			List <YmtProductFreezeStock> ymtProductFreezeStockList = new YmtProductFreezeStockWapper().selectByFreezeId(FreezeId);
			Map<String, Object> tgtSqlMap = new HashMap<String,Object>();
			tgtSqlMap.put("catalogId",catalogsId);
			tgtSqlMap.put("freezeId",FreezeId);
			tgtSqlMap.put("productId",productId);
			tgtSqlMap.put("activityId",activityStartId);
			tgtSqlMap.put("productInActivityId",productInActivityId);
			tgtSqlMap.put("sku",SKU);
			tgtSqlMap.put("freezeNum",0);
			
			Logger.verifyEquals(1, ymtProductFreezeStockList.size(),"SQL-YmtProductFreezeStock 个数为1");
			AssertService.assertResultEqual(tgtSqlMap, MapUtil.pojoToMap(ymtProductFreezeStockList.get(0)),"SQL-YmtProductFreezeStock");
			
			//SQL-Ymt_ProductFreezeStockLog
			List<YmtProductFreezeStockLog> ymtProductFreezeStockLogList = new YmtProductFreezeStockLogWapper().selectByFreezeId(FreezeId);
			Logger.verifyEquals(2, ymtProductFreezeStockLogList.size(),"SQL-Ymt_ProductFreezeStockLog 个数为2");
			
			for(YmtProductFreezeStockLog ymtProductFreezeStockLog:ymtProductFreezeStockLogList){
				tgtSqlMap.remove("sku");
				if (ymtProductFreezeStockLog.getOperateType()==1){
					tgtSqlMap.put("freezeNum",FreezeNum);
				}else{
					tgtSqlMap.put("freezeNum",FreezeNum*(-1));
				}
				AssertService.assertResultEqual(tgtSqlMap, MapUtil.pojoToMap(ymtProductFreezeStockLog),"SQL-Ymt_ProductFreezeStockLog");
			}
			
			//SQl-Ymt_Catalogs
			Logger.verifyEquals(catalogNum-FreezeNum,ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0).getiNum(),"SQl-Ymt_Catalogs iNum");
			
			//SQL-Ymt_ProductActivityStock
			ymtActivityStock = ymtActivityStockWapper.selectByProductId(productId).get(0);
			Logger.verifyEquals(activityNum+FreezeNum, ymtActivityStock.getIstocknum(), "SQL-Ymt_ProductActivityStock 商品库存数量");
			
			//check mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mongo-ProductStockInfo
				Logger.verifyEquals((catalogNum-FreezeNum),new ProductStockInfoWapper().getByCatalogId(catalogsId).get("StockNum"),"Mongo-ProductStockInfo 库存");
				
	
				//Mongo-ProductActivityStockInfo
				Map<String, Object> productActivityStockInfoMap = new ProductActivityStockInfoWapper().getByProductId(productId);
				Map<String, Object> tgtOldMongolMap2 = MapUtil.hashMap("ProductId", productId, //商品Id
																		"CatalogId",catalogsId, //商品规格Id
																		"ProductActivityId",productInActivityId, //商品活动Id
																		"ActivityStockNum",activityNum+FreezeNum,//活动库存
																		"ActivityId",activityStartId); //活动Id
				AssertService.assertResultEqual(tgtOldMongolMap2, productActivityStockInfoMap,"Mongo-ProductActivityStockInfo ");
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				CatalogBean catalogBean = new CatalogBean();
				CatalogBean.StockBean stockBean1 =catalogBean.new StockBean();
				stockBean1.setStock(catalogNum-FreezeNum);
				stockBean1.setStockType(0);
				
				CatalogBean.StockBean stockBean2 =catalogBean.new StockBean();
				stockBean2.setStock(activityNum+FreezeNum);
				stockBean2.setProductInActivityId(productInActivityId);
				stockBean2.setStockType(1);
				
				catalogBean.setCatalogId(catalogsId);
				catalogBean.setStockBean(Arrays.asList(stockBean1,stockBean2));
				ProductStockCallService.checkNewMongoCatalogStock(productId, Arrays.asList(catalogBean));
			}
			
			
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnfreezeAllocateStock_002() {
		Logger.start(true, "解冻调拨库存-进共享");
		try{
			//发布商品
			String productId = SellerProductCallService.addNewProductAndApproveActivity(sellerId,
					sellerName,activityUser,activityPswd,String.valueOf(activityStartId),null);
			
			int productInActivityId = new YmtProductsInActivityWapper().selectByProductId(productId).getIproductinactivityid();
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogsId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();//共享库存
			String SKU = ymtCatalog.getsSKU();
			
			YmtProductActivityStockWapper ymtActivityStockWapper = new YmtProductActivityStockWapper();
			YmtProductActivityStock ymtActivityStock = ymtActivityStockWapper.selectByProductId(productId).get(0);
			int activityNum = ymtActivityStock.getIstocknum();//活动库存
			
			
			//冻结库存
			String FreezeId = "DJ"+YMTDateUtil.getSysTime(dateformat);
			int FreezeNum = 1;
			
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("UserId",sellerId);
			updateMap.put("FreezeId",FreezeId);
			updateMap.put("ProductId",productId);
			updateMap.put("ActivityId",activityStartId);
			updateMap.put("ProductInActivityId",productInActivityId);
			updateMap.put("IsFbxProduct",false);
			updateMap.put("StoreType",0); //冻结库存
			updateMap.put("FreezeCatalogList[0]->CatalogId",catalogsId);
			updateMap.put("FreezeCatalogList[0]->Sku",SKU);
			updateMap.put("FreezeCatalogList[0]->FreezeNum",FreezeNum);
			
			JSONObject retBean = freezeAllocateStockCall.callAndGetReturnData(updateMap);
			Logger.verifyEquals( 200,retBean.getInt("Code"), "响应报文-返回码");
			
			//解冻库存
			updateMap.put("OperateType", 3);
			JSONObject unfreezeBean = unfreezeAllocateStockCall.callAndGetReturnData(updateMap);
			Logger.verifyEquals( 200,unfreezeBean.getInt("Code"), "响应报文-返回码");
			
			//check sql
			//SQL-YmtProductFreezeStock
			List<YmtProductFreezeStock> ymtProductFreezeStockList = new YmtProductFreezeStockWapper().selectByFreezeId(FreezeId);
			Map<String, Object> tgtSqlMap = new HashMap<String,Object>();
			tgtSqlMap.put("catalogId",catalogsId);
			tgtSqlMap.put("freezeId",FreezeId);
			tgtSqlMap.put("productId",productId);
			tgtSqlMap.put("activityId",activityStartId);
			tgtSqlMap.put("productInActivityId",productInActivityId);
			tgtSqlMap.put("sku",SKU);
			tgtSqlMap.put("freezeNum",0);

			Logger.verifyEquals(1, ymtProductFreezeStockList.size(),"SQL-YmtProductFreezeStock 个数为1");
			AssertService.assertResultEqual(tgtSqlMap, MapUtil.pojoToMap(ymtProductFreezeStockList.get(0)),"SQL-YmtProductFreezeStock");
			
			//SQL-Ymt_ProductFreezeStockLog
			List<YmtProductFreezeStockLog> ymtProductFreezeStockLogList = new YmtProductFreezeStockLogWapper().selectByFreezeId(FreezeId);
			Logger.verifyEquals(2, ymtProductFreezeStockLogList.size(),"SQL-Ymt_ProductFreezeStockLog 个数为2");
			
			for(YmtProductFreezeStockLog ymtProductFreezeStockLog:ymtProductFreezeStockLogList){
				tgtSqlMap.remove("sku");
				if (ymtProductFreezeStockLog.getOperateType()==1){
					tgtSqlMap.put("freezeNum",FreezeNum);
				}else{
					tgtSqlMap.put("freezeNum",FreezeNum*(-1));
				}
				AssertService.assertResultEqual(tgtSqlMap, MapUtil.pojoToMap(ymtProductFreezeStockLog),"SQL-Ymt_ProductFreezeStockLog");
			}
			
			//SQl-Ymt_Catalogs
			Logger.verifyEquals(catalogNum,ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0).getiNum(),"SQl-Ymt_Catalogs iNum");
			
			//SQL-Ymt_ProductActivityStock
			ymtActivityStock = ymtActivityStockWapper.selectByProductId(productId).get(0);
			Logger.verifyEquals(activityNum, ymtActivityStock.getIstocknum(), "SQL-Ymt_ProductActivityStock 商品库存数量");
			
			//check mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mongo-ProductStockInfo
				Logger.verifyEquals((catalogNum),new ProductStockInfoWapper().getByCatalogId(catalogsId).get("StockNum"),"Mongo-ProductStockInfo 库存");
				
	
				//Mongo-ProductActivityStockInfo
				Map<String, Object> productActivityStockInfoMap = new ProductActivityStockInfoWapper().getByProductId(productId);
				Map<String, Object> tgtOldMongolMap2 = MapUtil.hashMap("ProductId", productId, //商品Id
																		"CatalogId",catalogsId, //商品规格Id
																		"ProductActivityId",productInActivityId, //商品活动Id
																		"ActivityStockNum",activityNum,//活动库存
																		"ActivityId",activityStartId); //活动Id
				AssertService.assertResultEqual(tgtOldMongolMap2, productActivityStockInfoMap,"Mongo-ProductActivityStockInfo ");
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				CatalogBean catalogBean = new CatalogBean();
				CatalogBean.StockBean stockBean1 =catalogBean.new StockBean();
				stockBean1.setStock(catalogNum);
				stockBean1.setStockType(0);
				
				CatalogBean.StockBean stockBean2 =catalogBean.new StockBean();
				stockBean2.setStock(activityNum);
				stockBean2.setProductInActivityId(productInActivityId);
				stockBean2.setStockType(1);
				
				catalogBean.setCatalogId(catalogsId);
				catalogBean.setStockBean(Arrays.asList(stockBean1,stockBean2));
				ProductStockCallService.checkNewMongoCatalogStock(productId, Arrays.asList(catalogBean));
			}
			
			
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
